const {
  request
} = require('../../utils/request');
const {
  apiHost
} = require('../../utils/request');

/* 这个自定义组件js结构 */
Component({
  options: {
    multipleSlots: true // 在组件定义时的选项中启用多slot支持
  },
  properties: {
    // 这里定义了innerText属性，属性值可以在组件使用时指定
    innerText: {
      type: String,
      value: 'default value',
    }
  },
  data: {
    toDo: null,
    userInfo: {},
    someData: {},
    showLogin: false,
    showCheckPhone: false,
  },
  methods: {
    isAuthorired(toDo) {
      //判断用户是否授权,且是否有手机号（注册）
      var that = this;
      this.setData({
        toDo: toDo
      })
      wx.getSetting({
        success: function (res) {
          request('/wechat/miniprogram/user/info', 'GET', {}).then(function (res) {
            console.log(res)
            if (res.data.data.nickName == '' || wx.getStorageSync('userInfo').nickName == ''||res.data.data.nickName == '微信用户') {
              that.setData({
                showLogin: true,
              })
            } else {
              that.setData({
                showLogin: false,
              })
              that.data.toDo();
            }
          })
        }
      })
    },


    bindGetUserInfo(e) { //用户按了允许授权按钮
      if (e.detail.userInfo) {
        let that = this;
        that.setData({
          showLogin: false,
        })
        //授权注册
        request('/wechat/miniprogram/user/info', 'GET', {}).then(function (res) {
          that.data.toDo();
        })
      } else {
        //用户按了拒绝按钮
        wx.showModal({
          title: '警告',
          content: '您点击了拒绝授权，将无法进入小程序，请授权之后再进入!',
          showCancel: false,
          confirmText: '返回授权',
          success: function (res) {
            if (res.confirm) {
              console.log('用户点击了“返回授权”')
            }
          }
        })
      }
    },

    getUserProfile(e) {//用户按了允许授权按钮
      var that = this
      wx.getUserProfile({
        desc: '用于完善会员资料', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
        success: (res) => {
          console.log(res.userInfo)
          //更新头像等信息
          request('/wechat/miniprogram/user/userprofile', 'POST', res.userInfo).then(function (res) {
            that.setData({
              showLogin: false,
            })
            wx.setStorageSync('userInfo', res.data.data);
            getApp().globalData.data = {
              '_avatar': res.data.data.avatar,
              '_nickName': res.data.data.nickName
            }
            if(that.data.toDo=='getPhone'){
              that.selectComponent("#getPhone").isHavePhone()
            }else{
              that.data.toDo();
            }
          })
        },
        fail: (res) => {
          //用户按了拒绝按钮
          wx.showModal({
            title: '警告',
            content: '部分功能使用将受限',
            showCancel: false,
            confirmText: '返回授权',
            success: function (res) {
              if (res.confirm) {
                console.log('用户点击了“返回授权”')
              }
            }
          })
        }
      })
    },

  }
})